library(ggplot2)         #for data visualisation 
library(MetBrewer)       #for colour palettes
library(ragg)            #for saving data viz in png
library(showtext)        #for displaying fonts in data viz
library(tidyverse)       #for data-wrangling

##data-wrangling----
BrSenSwitch <- read.csv("BrSenSwitch.csv", sep = ";") %>%
               mutate(fusao = ifelse(is.na(fusao), 0, fusao)) %>%
               mutate(fissao = ifelse(is.na(fissao), 0, fissao)) %>%
               mutate(partido_novo = ifelse(is.na(partido_novo), 0, partido_novo)) %>%
               mutate(home_inverted = 1-home) %>%
               filter(partido_novo != 1,
                      fissao != 1) %>%
               mutate(to_be_elected = case_when(legislatura == 48 ~ 1,
                                                legislatura == 49 ~ 2,
                                                legislatura == 50 ~ 1,
                                                legislatura == 51 ~ 2,
                                                legislatura == 52 ~ 1,
                                                legislatura == 53 ~ 2,
                                                legislatura == 54 ~ 1,
                                                legislatura == 55 ~ 2,
                                                legislatura == 56 ~ 1))

double_renovation <- BrSenSwitch %>% filter(to_be_elected == 2) 
single_renovation <- BrSenSwitch %>% filter(to_be_elected == 1)

##fonts----
font_add(family = "regular", "Barlow Semi Condensed-Regular.ttf")
font_add(family = "bold", "BarlowSemiCondensed-SemiBold.ttf")
showtext_auto() 
##data-vis----
#Same syntax for the data manipulation for Figure 1 in the main document
br <- BrSenSwitch %>%
      mutate(fusao = ifelse(is.na(fusao), 0, fusao)) %>%
      mutate(partido_novo = ifelse(is.na(partido_novo), 0, partido_novo)) %>%
      filter(fusao != 1,
             partido_novo != 1) %>%
      select(senador, partido_inicial, partido_final, mes, ano, home, y) %>%
      group_by(senador, mes, partido_final, ano) %>%
      filter(home != y) %>%
      summarise(n = n()) %>%
      filter(partido_final != "PPR") %>%
      ungroup() %>%
      group_by(ano) %>%
      summarise(n = n())

br_null <- tribble(
~ano, ~n, 
1998, 0,
2008, 2,
2009, 5,
2010, 1,
2011, 2,
2012, 2,
2013, 3,
2014, 1,
)

br_compared <- br %>% rbind(br_null) %>%
                      mutate(anti_rule = ifelse(ano %in% c(2008, 2009, 2010, 2011,
                                                           2012, 2013, 2014), "Present", "Absent"))

my_palette_si <- met.brewer("Johnson", 2)

br_compared %>% ggplot(aes(ano, n, fill = anti_rule)) +
                geom_bar(stat = "identity", width = 0.9) +
                geom_text(aes(x = ano, y = n, fontface = 2, label = n, colour = anti_rule), 
                          position = position_dodge(width = 1), hjust = -0.10, size = 10) + 
                scale_colour_manual(values = c("#0086a8", "#d04e00")) +  scale_fill_manual(values = c("#0086a8", "#d04e00")) +  
                ylim(0, 25) +
                scale_x_continuous(limits = c(1988.5, 2023.5),
                                   breaks = seq(1990, 2022, by = 1)) +
                coord_flip(expand=FALSE) +
                guides(fill = guide_legend(reverse = F,
                                           title = "Anti-Defection Rule"),
                       override.aes = list(size = 10)) +
                guides(colour = guide_legend(reverse = F,
                                             title = "Anti-Defection Rule"),
                       override.aes = list(size = 10)) +
                xlab("") + ylab("Instances of Party Switching") + 
                theme(
                  #Title, Subtitle, Caption
                  plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 45, color = "black"),
                  plot.title.position = "plot",
                  plot.subtitle = element_text(family="regular", size = 25, hjust = 0.5, color = "black"),
                  plot.caption = element_text(family="bold", size = 25, color = "black", hjust = 1),
                  plot.caption.position = "plot",
                  #Panel and Background
                  panel.border = element_blank(),
                  panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank(),
                  panel.background = element_rect(fill = "#FFFFFF"),
                  plot.background = element_rect(fill = "#FFFFFF", linetype = 'blank'),
                  #Axes
                  axis.title = element_text(size = 40, family = "regular", color = "black"),
                  axis.text.y = element_text(size = 25, family = "bold", color = "black"),
                  axis.text.x = element_text(size = 30, family = "regular", color = "black"),
                  axis.ticks = element_blank(),
                  axis.line = element_blank(),
                  #Plus
                  text = element_text(family = "regular", size = 45),
                  legend.position = "right",
                  legend.title = element_text(size = 30, family = "bold"),
                  legend.text = element_text(size = 30, family = "regular"),
                  legend.key = element_rect(fill = "#FFFFFF"),
                  legend.key.width = unit(0.5, "cm"),
                  legend.background = element_rect(fill = "#FFFFFF"),
                  legend.spacing.x = unit(0.2, 'cm')) -> attention

ggsave("SuppMat.1.png",
       plot = attention,
       device = agg_png(width = 6, height = 4, units = "in", res = 300))

#t-test
with <- br_compared %>% filter(anti_rule == "Present") %>% select(n)
without <- br_compared %>% filter(anti_rule == "Absent") %>% select(n)

t.test(with, without)
